//
//  AXFTabBarController.m
//  AIXF
//
//  Created by Macx on 2017/7/9.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFTabBarController.h"
#import "AXFNavigationController.h"
#import "AXFShoppingCarViewController.h"
#import "AXFAccountManager.h"

@interface AXFTabBarController ()<UITabBarControllerDelegate>


@property (nonatomic,assign)NSInteger indexFlag;

@end

@implementation AXFTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    UIViewController *vc1 = [self addChildViewContollerWithClassName:@"AXFHomeViewController" andTabBarItemWithImageName:@"v2_home" andTabBarItemWithTitle:@"首页"];
    
    UIViewController *vc2 = [self addChildViewContollerWithClassName:@"AXFSuperMarketViewController" andTabBarItemWithImageName:@"v2_order" andTabBarItemWithTitle:@"闪电超市"];
    
    UIViewController *vc3 = [self addChildViewContollerWithClassName:@"AXFShoppingCarViewController" andTabBarItemWithImageName:@"shopCart" andTabBarItemWithTitle:@"购物车"];
    
    UIViewController *vc4 = [self addChildViewContollerWithClassName:@"AXFMineViewController" andTabBarItemWithImageName:@"v2_my" andTabBarItemWithTitle:@"我的"];
    
    self.viewControllers = @[vc1, vc2, vc3 ,vc4];
    
    self.tabBar.translucent = NO;
    
    self.tabBar.tintColor = [UIColor grayColor];
    
    self.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiceGoodsNumNotification:) name:kChangeOrderNumNotification object:nil];
}

- (void)didReceiceGoodsNumNotification:(NSNotification *)noti
{
    NSInteger goodsNum = _AXFAccountManager.goodsNum;
    
    if (goodsNum > 0)
    {
        [[self.tabBar.items objectAtIndex:2] setBadgeValue:[NSString stringWithFormat:@"%zd",goodsNum]];
    }else
    {
        [[self.tabBar.items objectAtIndex:2] setBadgeValue:nil];
    }
    
    
    self.orderNum = goodsNum;
       
}



- (UIViewController *)addChildViewContollerWithClassName:(NSString *)className andTabBarItemWithImageName:(NSString *)imageName andTabBarItemWithTitle:(NSString *)title
{
    Class class = NSClassFromString(className);

    UIViewController *vc = [[class alloc] init];

    vc.tabBarItem.image = [UIImage imageNamed:imageName];

    NSString *selImageName = [imageName stringByAppendingString:@"_r"];
    
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    AXFNavigationController *nav = [[AXFNavigationController alloc] initWithRootViewController:vc];
   
    vc.title = title;
  
    return nav;
}



- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    NSInteger index = [self.tabBar.items indexOfObject:item];
    
    if (self.indexFlag != index) {
        [self animationWithIndex:index];
    }
        
}

// 动画效果
- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.08;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer]
     addAnimation:pulse forKey:nil];
    self.indexFlag = index;
    
    

}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    AXFNavigationController *nav = (AXFNavigationController *)viewController;
    
    if ([[nav.viewControllers objectAtIndex:0] isKindOfClass:[AXFShoppingCarViewController class]])
    {
//           UIViewController *vc3 ;
//        if (self.orderNum > 0)
//        {
//            
//            vc3 = [self addChildViewContollerWithClassName:@"AXFGoodsViewController" andTabBarItemWithImageName:@"shopCart" andTabBarItemWithTitle:@"购物车"];
//        }else
//        {
//            vc3 = [self addChildViewContollerWithClassName:@"AXFShoppingCarViewController" andTabBarItemWithImageName:@"shopCart" andTabBarItemWithTitle:@"购物车"];
//        }
        
        
      UIViewController *vc3 = [self addChildViewContollerWithClassName:@"AXFGoodsViewController" andTabBarItemWithImageName:@"shopCart" andTabBarItemWithTitle:@"购物车"];
        
        [self presentViewController:vc3 animated:YES completion:nil];
        
        return NO;
    }
    return YES;
    
    
}


@end
